| Author | |
|---|---|
| Name | Claire Descombes |
| Affiliation | Universitätsklinik für Neurochirurgie, Inselspital Bern |
| Degree | MSc Statistics and Data Science, University of Bern |
| Contact | claire.descombes@insel.ch |
The reference material for this course, as well as some useful literature to deepen your knowledge of R, can be found at the bottom of the page.
When you get a file from somewhere on your computer (e.g. a dataset), you can either
The advantage of putting the files in the folder that contains your script and is set as the working directory is that you can easily move the folder around on your computer without getting any problems with your script: just set the working directory to your source file every time you open it, and you’ll be fine.
# Example
setwd("~/path/to/your/folder/")
data <- read.csv("testdata.csv")
The advantage of always giving the full path to a file is that you can get data in different folders on your computer, avoiding things like copying the source data in every folder where you have a corresponding script.
# Example
data <- read.csv("~/path/to/your/folder/testdata.csv")
To find out what your current working directory is, you can use the
function getwd().
getwd()
## [1] "/home/claire/Documents/GitHub/rforphysicians/docs"
Working directory
To tell R which folder you are working in (e.g., where your data is stored), you have several options:
setwd("path/to/your/folder") in your script.💡 I recommend placing both your script and your data files in the same folder, and setting that folder as your working directory. This helps avoid errors caused by R not finding your data.
getwd() # Displays the current working directory
setwd("path/to/your/folder") # Sets the working directory
We will first look at how to import a CSV file into R as a data frame.
CSV stands for Comma-Separated Values. In a .csv file,
the values are stored as plain text, separated by commas. This is a
simple and widely used format for storing tabular data.
After setting your working directory or determining the path to your
CSV file, you can use the read.csv() function to import the
data. This will create a data frame, which is one of the most commonly
used structures in R for handling datasets.
# Import a CSV file into a data frame
dataset <- read.csv("~/path/to/your/folder/data.csv")
💡 I recommend using data frames — they are generally easier to work with than matrices, especially for beginners.
Another widely used data format is the Excel file
(.xlsx). For these, you can use the readxl
package to import the data:
# Load the readxl package
library(readxl)
# Read the first sheet of an Excel file
dataset <- read_excel("~/path/to/your/folder/data.xlsx")
⚠️ Note: If your file is actually a CSV but mistakenly has a .xlsx extension, you should rename it to .csv and use read.csv() instead. Mixing up formats can lead to import errors.
Let us now look at real data frames to learn how to call or modify
their elements. To do this, we will use multiple health data sets from
the National Health and Nutrition Examination (NHANES) Survey
from 2011-2012. The survey assessed overall health and nutrition of
adults and children in the United States and was conducted by the
National Center for Health Statistics (NCHS). The data sets can be found
in the data_sets folder
folder.
| Dataset | NHANES Code | Description | CSV File |
|---|---|---|---|
| Demographics | DEMO_G | Age, sex, race/ethnicity, income, education | DEMO_G.csv |
| Blood Pressure | BPX_G | Systolic/diastolic blood pressure, number of readings | BPX_G.csv |
| Body Measures | BMX_G | Height, weight, BMI, waist circumference | BMX_G.csv |
| Smoking Questionnaire | SMQ_G | Smoking habits, exposure to secondhand smoke | SMQ_G.csv |
# Load the necessary CSV files into data frames
demo <- read.csv("/home/claire/Documents/GitHub/rforphysicians/data_sets/DEMO_G.csv") # Demographics (cycle G = 2011–2012)
bpx <- read.csv("/home/claire/Documents/GitHub/rforphysicians/data_sets/BPX_G.csv") # Blood pressure
bmx <- read.csv("/home/claire/Documents/GitHub/rforphysicians/data_sets/BMX_G.csv") # Body measures
smq <- read.csv("/home/claire/Documents/GitHub/rforphysicians/data_sets/SMQ_G.csv") # Smoking questionnaire
# Check the structure of the data frames
str(demo)
## 'data.frame': 9756 obs. of 48 variables:
## $ SEQN : int 62161 62162 62163 62164 62165 62166 62167 62168 62169 62170 ...
## $ SDDSRVYR: chr "NHANES 2011-2012 public release" "NHANES 2011-2012 public release" "NHANES 2011-2012 public release" "NHANES 2011-2012 public release" ...
## $ RIDSTATR: chr "Both interviewed and MEC examined" "Both interviewed and MEC examined" "Both interviewed and MEC examined" "Both interviewed and MEC examined" ...
## $ RIAGENDR: chr "Male" "Female" "Male" "Female" ...
## $ RIDAGEYR: int 22 3 14 44 14 9 0 6 21 15 ...
## $ RIDAGEMN: int NA NA NA NA NA NA 11 NA NA NA ...
## $ RIDRETH1: chr "Non-Hispanic White" "Mexican American" "Other Race - Including Multi-Racial" "Non-Hispanic White" ...
## $ RIDRETH3: chr "Non-Hispanic White" "Mexican American" "Non-Hispanic Asian" "Non-Hispanic White" ...
## $ RIDEXMON: chr "May 1 through October 31" "November 1 through April 30" "May 1 through October 31" "November 1 through April 30" ...
## $ RIDEXAGY: int NA 3 14 NA 14 10 NA 6 NA 15 ...
## $ RIDEXAGM: int NA 41 177 NA 179 120 12 81 NA 181 ...
## $ DMQMILIZ: chr "No" NA NA "Yes" ...
## $ DMQADFC : chr NA NA NA "No" ...
## $ DMDBORN4: chr "Born in 50 US states or Washington, DC" "Born in 50 US states or Washington, DC" "Born in 50 US states or Washington, DC" "Born in 50 US states or Washington, DC" ...
## $ DMDCITZN: chr "Citizen by birth or naturalization" "Citizen by birth or naturalization" "Citizen by birth or naturalization" "Citizen by birth or naturalization" ...
## $ DMDYRSUS: chr NA NA NA NA ...
## $ DMDEDUC3: chr NA NA "8th grade" NA ...
## $ DMDEDUC2: chr "High school graduate/GED or equivalent" NA NA "Some college or AA degree" ...
## $ DMDMARTL: chr "Never married" NA NA "Married" ...
## $ RIDEXPRG: chr NA NA NA "The participant was not pregnant at exam" ...
## $ SIALANG : chr "English" "English" "English" "English" ...
## $ SIAPROXY: chr "Yes" "Yes" "Yes" "No" ...
## $ SIAINTRP: chr "No" "No" "No" "No" ...
## $ FIALANG : chr "English" "English" "English" "English" ...
## $ FIAPROXY: chr "No" "No" "No" "No" ...
## $ FIAINTRP: chr "No" "No" "No" "No" ...
## $ MIALANG : chr "English" NA "English" NA ...
## $ MIAPROXY: chr "No" NA "No" NA ...
## $ MIAINTRP: chr "No" NA "No" NA ...
## $ AIALANGA: chr "English" NA "English" NA ...
## $ WTINT2YR: num 102641 15458 7398 127351 12210 ...
## $ WTMEC2YR: num 104237 16116 7869 127965 13384 ...
## $ SDMVPSU : int 1 3 3 1 2 1 2 2 1 3 ...
## $ SDMVSTRA: int 91 92 90 94 90 91 92 103 92 91 ...
## $ INDHHIN2: chr "$75,000 to $99,999" "$15,000 to $19,999" "$100,000 and Over" "$45,000 to $54,999" ...
## $ INDFMIN2: chr "$75,000 to $99,999" "$15,000 to $19,999" "$100,000 and Over" "$45,000 to $54,999" ...
## $ INDFMPIR: num 3.15 0.6 4.07 1.67 0.57 NA NA 3.48 0.33 5 ...
## $ DMDHHSIZ: int 5 6 5 5 5 6 7 5 5 4 ...
## $ DMDFMSIZ: int 5 6 5 5 5 6 4 5 5 4 ...
## $ DMDHHSZA: int 0 2 0 1 1 0 3 0 0 0 ...
## $ DMDHHSZB: int 1 2 2 2 2 4 3 2 1 2 ...
## $ DMDHHSZE: int 0 0 1 0 0 0 1 1 0 0 ...
## $ DMDHRGND: chr "Female" "Female" "Male" "Male" ...
## $ DMDHRAGE: int 50 24 42 52 33 44 61 43 51 38 ...
## $ DMDHRBR4: chr "Born in 50 US states or Washington, DC" "Born in 50 US states or Washington, DC" "Born in 50 US states or Washington, DC" "Born in 50 US states or Washington, DC" ...
## $ DMDHREDU: chr "College Graduate or above" "High School Grad/GED or Equivalent" "College Graduate or above" "Some College or AA degree" ...
## $ DMDHRMAR: chr "Married" "Living with partner" "Married" "Married" ...
## $ DMDHSEDU: chr "College Graduate or above" NA "Some College or AA degree" "Some College or AA degree" ...
str(bpx)
## 'data.frame': 9338 obs. of 27 variables:
## $ SEQN : int 62161 62162 62163 62164 62165 62166 62167 62168 62169 62170 ...
## $ PEASCST1: chr "Complete" "Complete" "Complete" "Complete" ...
## $ PEASCTM1: int 596 64 788 527 468 583 55 98 1005 625 ...
## $ PEASCCT1: chr NA NA NA NA ...
## $ BPXCHR : int NA 100 NA NA NA NA 100 96 NA NA ...
## $ BPQ150A : chr "No" NA "Yes" "Yes" ...
## $ BPQ150B : chr "No" NA "No" "No" ...
## $ BPQ150C : chr "No" NA "No" "No" ...
## $ BPQ150D : chr "No" NA "No" "No" ...
## $ BPAARM : chr "Right" NA "Right" "Right" ...
## $ BPACSZ : chr "Large (15X32)" NA "Adult (12X22)" "Adult (12X22)" ...
## $ BPXPLS : int 82 NA 72 82 70 90 NA NA 72 62 ...
## $ BPXPULS : chr "Regular" "Regular" "Regular" "Regular" ...
## $ BPXPTY : chr "Radial" NA "Radial" "Radial" ...
## $ BPXML1 : int 130 NA 140 140 130 120 NA NA 140 140 ...
## $ BPXSY1 : int 110 NA 112 116 110 96 NA NA 124 124 ...
## $ BPXDI1 : int 82 NA 38 56 64 32 NA NA 80 82 ...
## $ BPAEN1 : chr "No" NA "No" "No" ...
## $ BPXSY2 : int 104 NA 108 118 104 94 NA NA 126 122 ...
## $ BPXDI2 : int 68 NA 36 66 72 40 NA NA 74 84 ...
## $ BPAEN2 : chr "No" NA "No" "No" ...
## $ BPXSY3 : int 118 NA 106 120 106 94 NA NA 124 128 ...
## $ BPXDI3 : int 74 NA 38 58 78 0 NA NA 80 82 ...
## $ BPAEN3 : chr "No" NA "No" "No" ...
## $ BPXSY4 : int NA NA NA NA NA NA NA NA NA NA ...
## $ BPXDI4 : int NA NA NA NA NA NA NA NA NA NA ...
## $ BPAEN4 : chr NA NA NA NA ...
str(bmx)
## 'data.frame': 9338 obs. of 26 variables:
## $ SEQN : int 62161 62162 62163 62164 62165 62166 62167 62168 62169 62170 ...
## $ BMDSTATS: int 1 1 1 1 1 1 1 1 1 1 ...
## $ BMXWT : num 69.2 12.7 49.4 67.2 69.1 28.8 10.8 23.6 54.6 63.5 ...
## $ BMIWT : int NA NA NA NA NA NA NA NA NA NA ...
## $ BMXRECUM: num NA 95.7 NA NA NA NA 79.5 NA NA NA ...
## $ BMIRECUM: int NA NA NA NA NA NA NA NA NA NA ...
## $ BMXHEAD : num NA NA NA NA NA NA NA NA NA NA ...
## $ BMIHEAD : logi NA NA NA NA NA NA ...
## $ BMXHT : num 172.3 94.7 168.9 170.1 159.4 ...
## $ BMIHT : int NA NA NA NA NA NA NA NA NA NA ...
## $ BMXBMI : num 23.3 14.2 17.3 23.2 27.2 16.2 NA 15.4 20.1 18.2 ...
## $ BMDBMIC : int NA 2 2 NA 3 2 NA 2 NA 2 ...
## $ BMXLEG : num 40.2 NA 40.3 40.5 42.1 31 NA NA 38.7 43.3 ...
## $ BMILEG : int NA NA NA NA NA NA NA NA NA NA ...
## $ BMXARML : num 35 18.5 36.3 37.2 35.2 28 16.2 24.8 33.4 37.5 ...
## $ BMIARML : int NA NA NA NA NA NA NA NA NA NA ...
## $ BMXARMC : num 32.5 16.6 22 29.3 29.7 19.1 15.5 17.1 28.5 25.8 ...
## $ BMIARMC : int NA NA NA NA NA NA NA NA NA NA ...
## $ BMXWAIST: num 81 45.4 64.6 80.1 86.7 59.8 NA 54.4 69.6 69.4 ...
## $ BMIWAIST: int NA NA NA NA NA NA NA NA NA NA ...
## $ BMXSAD1 : num 17.7 NA 15.6 18.3 21 13.5 NA NA 16.4 14.8 ...
## $ BMXSAD2 : num 17.9 NA 15.5 18.5 20.8 13.5 NA NA 16.3 14.7 ...
## $ BMXSAD3 : num NA NA NA NA NA NA NA NA NA NA ...
## $ BMXSAD4 : num NA NA NA NA NA NA NA NA NA NA ...
## $ BMDAVSAD: num 17.8 NA 15.6 18.4 20.9 13.5 NA NA 16.4 14.8 ...
## $ BMDSADCM: int NA NA NA NA NA NA NA NA NA NA ...
str(smq)
## 'data.frame': 6790 obs. of 30 variables:
## $ SEQN : int 62161 62163 62164 62165 62169 62170 62171 62172 62174 62176 ...
## $ SMQ020 : chr "No" NA "No" NA ...
## $ SMD030 : int NA NA NA NA NA NA NA 28 NA NA ...
## $ SMQ040 : chr NA NA NA NA ...
## $ SMQ050Q : int NA NA NA NA NA NA NA NA NA NA ...
## $ SMQ050U : chr NA NA NA NA ...
## $ SMD055 : int NA NA NA NA NA NA NA NA NA NA ...
## $ SMD057 : int NA NA NA NA NA NA NA NA NA NA ...
## $ SMQ077 : chr NA NA NA NA ...
## $ SMD641 : int NA NA NA NA NA NA NA 30 NA NA ...
## $ SMD650 : int NA NA NA NA NA NA NA 10 NA NA ...
## $ SMD093 : chr NA NA NA NA ...
## $ SMDUPCA : chr "" "" "" "" ...
## $ SMD100BR: chr "" "" "" "" ...
## $ SMD100FL: chr NA NA NA NA ...
## $ SMD100MN: chr NA NA NA NA ...
## $ SMD100LN: chr NA NA NA NA ...
## $ SMD100TR: int NA NA NA NA NA NA NA 6 NA NA ...
## $ SMD100NI: num NA NA NA NA NA NA NA 0.6 NA NA ...
## $ SMD100CO: int NA NA NA NA NA NA NA 6 NA NA ...
## $ SMQ621 : chr NA "I have never smoked, not even a puff" NA "I have never smoked, not even a puff" ...
## $ SMD630 : int NA NA NA NA NA NA NA NA NA NA ...
## $ SMQ660 : chr NA NA NA NA ...
## $ SMQ664M : chr NA NA NA NA ...
## $ SMQ664C : chr NA NA NA NA ...
## $ SMQ664W : chr NA NA NA NA ...
## $ SMQ664B : logi NA NA NA NA NA NA ...
## $ SMQ664O : chr NA NA NA NA ...
## $ SMQ670 : chr NA NA NA NA ...
## $ SMAQUEX2: chr "Home Interview (20+ Yrs)" "A-CASI (12 - 19 Yrs)" "Home Interview (20+ Yrs)" "A-CASI (12 - 19 Yrs)" ...
✏️ Exercise on the NHANES data sets n°1: import the
demo, bpx, bmx and
smq data sets from the data_sets folder
folder into R.
💡 The codebook for each dataset can be accessed either on the NCHS website
or directly in R using the function
nhanesCodebook(nh_table, colname) from the package
nhanesA (which I used to download the data). You’ll find
more details about installing packages at the end of this chapter.
Being able to access elements in a data frame is essential when working with data. Here are some common methods to select specific elements, rows, or columns.
# Look at the first respectively last few rows
head(demo)
tail(demo)
# Select columns by name
demo[, c("RIDAGEYR", "RIAGENDR")] # Selecting age in years and gender
vars <- c("RIDAGEYR", "RIAGENDR")
demo[, vars] # Alternative using variable `vars`
# Select elements by position
demo[1, 1] # Access the first element of the first column (the respondent sequence number of the 1st participant)
## [1] 62161
ind_mat <- cbind(c(1, 3, 5), c(2, 4, 6))
demo[ind_mat] # Access rows and columns using multiple indices
## [1] "NHANES 2011-2012 public release" "Male"
## [3] NA
# Select rows based on a condition
head(demo[, "RIDAGEYR"] > 50) # Logical condition for age greater than 50
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
head(!(demo[, "DMDHHSIZ"] > 3)) # Logical negation for total number of people in the household not greater than 3
## [1] FALSE FALSE FALSE FALSE FALSE FALSE
demo[demo[, "RIDAGEYR"] > 50, ] # Rows where age > 50
demo[demo[, "DMDHHSIZ"] < 3, ] # Rows where total number of people in the household greater than 3
demo[demo[, "DMDHHSIZ"] >= 3, ] # Rows where total number of people in the household greater or equal 3